package com.wj.auth.service.impl;

import com.wj.auth.common.util.IdGenerator;
import com.wj.auth.dto.UserDTO;
import com.wj.auth.infrastrcture.entity.SUserEntity;
import com.wj.auth.infrastrcture.mapper.UserMapper;
import com.wj.auth.infrastrcture.repository.UserRepository;
import com.wj.auth.service.UserService;
import com.wj.auth.vo.UserInfo;
import com.wj.auth.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserMapper userMapper;

    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);

    @Override
    public UserVO getUserByUsername(String username) {
        UserVO userVO = userMapper.getUserByUsername(username);
        return userVO;
    }

    /*public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);
        String password = "123456";
        password = encoder.encode(password);
        System.out.println(password);
    }*/

    @Override
    public void insertUser(UserDTO userDTO) {
        SUserEntity userEntity = new SUserEntity();
        String password = userDTO.getPassword();
        password = encoder.encode(password);
        BeanUtils.copyProperties(userDTO, userEntity);
        Long id = IdGenerator.nextId();
        userEntity.setId(id);
        userEntity.setPassword(password);
        userEntity.setCreateTime(new Date());
        Long operatorId = userDTO.getUserId();
        userEntity.setCreateUser(operatorId);
        userEntity.setUpdateTime(new Date());
        userEntity.setUpdateUser(operatorId);
        userEntity.setDeleted(false);
        userEntity.setStatus(1);
        userRepository.save(userEntity);
    }

    @Override
    public List<UserInfo> getUserList() {
        List<UserInfo> userList = userMapper.getUserList();
        return userList;
    }
}